{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f75ef3ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "from snownlp import SnowNLP\n",
    "from nltk.sentiment.vader import SentimentIntensityAnalyzer\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "\n",
    "def snownlp_sa(sentence):\n",
    "    s = SnowNLP(sentence)\n",
    "    return s.sentiments\n",
    "\n",
    "def calc_df_sa_snownlp():\n",
    "    covid = 'clean.csv'\n",
    "    df = pd.read_csv(covid, index_col=0)\n",
    "    df['sa'] = df['text'].apply(snownlp_sa)\n",
    "    df.to_csv('sa.csv')\n",
    "    return df\n",
    "\n",
    "\n",
    "def nltk_sa_score(sentence):\n",
    "    sentiment_dict = sia.polarity_scores(sentence)\n",
    "    return sentiment_dict['pos'] - sentiment_dict['neg']\n",
    "\n",
    "\n",
    "def nltk_sa_classification(sentence):\n",
    "    sentiment_dict = sia.polarity_scores(sentence)\n",
    "    score = sentiment_dict['compound']\n",
    "    if score >= 0.05:\n",
    "        return 1\n",
    "    elif score <= -0.05:\n",
    "        return -1\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "\n",
    "def calc_df_sa_nltk():\n",
    "    sia = SentimentIntensityAnalyzer()\n",
    "    \n",
    "    covid = 'clean.csv'\n",
    "    df = pd.read_csv(covid, index_col=0)\n",
    "    df['sa'] = df['text'].apply(nltk_sa_score)\n",
    "    df['sa_label'] = df['text'].apply(nltk_sa_classification)\n",
    "    df.to_csv('sa.csv')\n",
    "    return df\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    df = calc_df_sa_nltk()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "36bc5f75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_name</th>\n",
       "      <th>user_location</th>\n",
       "      <th>date</th>\n",
       "      <th>text</th>\n",
       "      <th>sa</th>\n",
       "      <th>sa_label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ᏉᎥ☻լꂅϮ</td>\n",
       "      <td>astroworld</td>\n",
       "      <td>2020-07-25 12:27:21</td>\n",
       "      <td>smelled the scent hand sanitizers today someon...</td>\n",
       "      <td>0.186</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Tom Basile 🇺🇸</td>\n",
       "      <td>New York, NY</td>\n",
       "      <td>2020-07-25 12:27:17</td>\n",
       "      <td>hey and wouldn have made more sense have the p...</td>\n",
       "      <td>0.103</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Time4fisticuffs</td>\n",
       "      <td>Pewee Valley, KY</td>\n",
       "      <td>2020-07-25 12:27:14</td>\n",
       "      <td>trump never once claimed #covid hoax all claim...</td>\n",
       "      <td>-0.174</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ethel mertz</td>\n",
       "      <td>Stuck in the Middle</td>\n",
       "      <td>2020-07-25 12:27:10</td>\n",
       "      <td>the one gift #covid give appreciation for the ...</td>\n",
       "      <td>0.341</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>DIPR-J&amp;K</td>\n",
       "      <td>Jammu and Kashmir</td>\n",
       "      <td>2020-07-25 12:27:08</td>\n",
       "      <td>july medium bulletin novel #coronavirusupdates...</td>\n",
       "      <td>0.315</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         user_name         user_location                 date  \\\n",
       "0           ᏉᎥ☻լꂅϮ            astroworld  2020-07-25 12:27:21   \n",
       "1    Tom Basile 🇺🇸          New York, NY  2020-07-25 12:27:17   \n",
       "2  Time4fisticuffs      Pewee Valley, KY  2020-07-25 12:27:14   \n",
       "3      ethel mertz  Stuck in the Middle   2020-07-25 12:27:10   \n",
       "4         DIPR-J&K     Jammu and Kashmir  2020-07-25 12:27:08   \n",
       "\n",
       "                                                text     sa  sa_label  \n",
       "0  smelled the scent hand sanitizers today someon...  0.186         1  \n",
       "1  hey and wouldn have made more sense have the p...  0.103         1  \n",
       "2  trump never once claimed #covid hoax all claim... -0.174        -1  \n",
       "3  the one gift #covid give appreciation for the ...  0.341         1  \n",
       "4  july medium bulletin novel #coronavirusupdates...  0.315         1  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from snownlp import SnowNLP\n",
    "from nltk.sentiment.vader import SentimentIntensityAnalyzer\n",
    "import pandas as pd\n",
    "covid = 'sa.csv'\n",
    "df = pd.read_csv(covid, index_col=0)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9eb03898",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sa</th>\n",
       "      <th>sa_label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>111367.000000</td>\n",
       "      <td>111367.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.029995</td>\n",
       "      <td>0.111317</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.214615</td>\n",
       "      <td>0.818106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-0.080000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.178000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  sa       sa_label\n",
       "count  111367.000000  111367.000000\n",
       "mean        0.029995       0.111317\n",
       "std         0.214615       0.818106\n",
       "min        -1.000000      -1.000000\n",
       "25%        -0.080000      -1.000000\n",
       "50%         0.000000       0.000000\n",
       "75%         0.178000       1.000000\n",
       "max         1.000000       1.000000"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d078e0a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 141900 entries, 0 to 179107\n",
      "Data columns (total 6 columns):\n",
      " #   Column         Non-Null Count   Dtype  \n",
      "---  ------         --------------   -----  \n",
      " 0   user_name      141900 non-null  object \n",
      " 1   user_location  141900 non-null  object \n",
      " 2   date           141900 non-null  object \n",
      " 3   text           141900 non-null  object \n",
      " 4   sa             141900 non-null  float64\n",
      " 5   sa_label       141900 non-null  int64  \n",
      "dtypes: float64(1), int64(1), object(4)\n",
      "memory usage: 7.6+ MB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
